#! /usr/bin/env python3
import sys
from solid import *
import os
import solid.utils as u
import sys

sys.path.append("C://Users/DELL/Documents/OpenSCAD/libraries")


# https://github.com/JohK/nutsnbolts

class Platform:
    def __init__(self):
        self.nutsn = import_scad('./nutsnbolts-master/cyl_head_bolt.scad')
        self.res_dir = "./result"
    
    def gen_stl(self,mod,name):
        f = f'{self.res_dir}/{name}.scad'
        f = os.path.abspath(f)
        print(f)
        scad_render_to_file(mod, f)
        # os.system(f"openscad {f}")
        os.system(f"openscad -o {self.res_dir}/{name}.stl {f}")
    
    def bolt10_nut12(self):
        # data-metric_cyl_head_bolts.scad 查阅型号
        # b1 = self.nutsn.screw("M8x25", thread="modeled")
        b1 = self.nutsn.screw("M10x35", thread="modeled")
        b2 = translate([20, 0, 0])(b1)
        b3=rotate([0,180,0])(b2)
        # nut1 = self.nutsn.nut("M10", thread="modeled")
        nut1 = self.nutsn.nut("M12", thread="modeled")
        self.gen_stl(b3,"m12b")
        self.gen_stl(nut1,"m10n")

    def bolt8_nut10(self):
        # data-metric_cyl_head_bolts.scad 查阅型号
        b1 = self.nutsn.screw("M8x25", thread="modeled")
        b2 = translate([20, 0, 0])(b1)
        b3 = rotate([0, 180, 0])(b2)
        nut1 = self.nutsn.nut("M10", thread="modeled")
        self.gen_stl(b3, "screw")
        self.gen_stl(nut1, "nut")
    def leg_with_thread_hole(self):
        cy1 = cylinder(r=8, h=30)
        cy2 = cylinder(r=5, h=30)
        cy3 = cy1 - cy2
        cy4 = rotate([180, 0, 0])(cy3)
        nut1 = self.nutsn.nut("M8", thread="modeled")
        assem = cy4 + nut1
        


if __name__ == '__main__':
    p = Platform()
    # p.bolt8_nut10()
    p.bolt10_nut12()
